#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/10/29 22:44
# @Author  : Nabby
# @File    : user_api.py
# @Desc    :

from app import app, request
from infrastructure.utils.decorator.oauth2_tool import oauth2_check
from application.user_manage import UserManage
from infrastructure.utils.http import responser
from infrastructure.utils.decorator.oauth2_tool import oauth2_check

@app.route('/api/get_user_list', endpoint='get_user_list', methods=['POST'])
@oauth2_check
def get_user_list():
    res_status, rjson = responser.post_param_check(request, [
        'userId',
        'pageSize',
        'page'
    ])
    if res_status == 'success':
        return UserManage.get_user_list(
            username=rjson.get('username'),
            company_name=rjson.get('companyName'),
            phone=rjson.get('phone'),
            email=rjson.get('email'),
            page=int(rjson.get('page', 1)),
            page_size=int(rjson.get('pageSize', 999))
        )
    else:
        return rjson


@app.route('/api/register', endpoint='register', methods=['POST'])
def add_user():
    res_status, rjson = responser.post_param_check(request, [
        'username',
        'password',
        'country',
        'companyName',
        'email',
        'roleId'
    ])
    if res_status == 'success':
        return UserManage.add_user(
            username=rjson.get('username'),
            password=rjson.get('password'),
            country=rjson.get('country'),
            companyName=rjson.get('companyName'),
            email=rjson.get('email'),
            role_id=rjson.get('roleId'),
            phone=rjson.get('phone'),
        )
    else:
        return rjson
     

@app.route('/api/update_user', endpoint='update_user', methods=['POST'])
@oauth2_check
def update_user():
    res_status, rjson = responser.post_param_check(request, [
        'id',
        'username',
        'country',
        'companyName',
        'email',
        'roleId',
        'phone'
    ])
    if res_status == 'success':
        return UserManage.update_user(
            id=rjson.get("id"),
            username=rjson.get('username'),
            country=rjson.get('country'),
            companyName=rjson.get('companyName'),
            email=rjson.get('email'),
            role_id=rjson.get('roleId'),
            phone=rjson.get('phone'),
        )
    else:
        return rjson


@app.route('/api/update_user_password', endpoint='update_user_password', methods=['POST'])
@oauth2_check
def update_user_password():
    res_status, rjson = responser.post_param_check(request, [
        'id',
        'oldPassword',
        'newPassword'
    ])
    if res_status == 'success':
        return UserManage.update_user_password(
            id=rjson.get("id"),
            old_password=rjson.get("oldPassword"),
            new_password=rjson.get("newPassword")
        )
    else:
        return rjson


@app.route('/api/change_user_status', endpoint='change_user_status', methods=['POST'])
@oauth2_check
def change_user_status():
    res_status, rjson = responser.post_param_check(request, [
        'userId',
        'status'
    ])
    if res_status == 'success':
        return UserManage.change_user_status(
            user_id=rjson.get("userId"),
            status=rjson.get("status")
        )
    else:
        return rjson


@app.route('/api/delete_user', endpoint='delete_user', methods=['POST'])
@oauth2_check
def delete_user():
    res_status, rjson = responser.post_param_check(request, [
        'id'
    ])
    if res_status == 'success':
        return UserManage.delete_user(
            user_id=rjson.get("id")
        )
    else:
        return rjson

@app.route('/api/login', endpoint='login', methods=['POST'])
def login():
    res_status, rjson = responser.post_param_check(request, [
        'userName',
        'password'
    ])
    if res_status == 'success':
        return UserManage.login(
            userName=rjson.get("userName"),
            password=rjson.get("password")
        )
    else:
        return rjson